package models

import (
	"SlowAdmin/bootstrap"
	"SlowAdmin/pkg/utils"
	"time"
)

type BaseModel struct {
}

type TreeOptions struct {
	Label    string        `json:"label"`
	Key      string        `json:"key"`
	Children []TreeOptions `json:"children"`
}

var Db = bootstrap.Db

// @title 列表时间格式化
func DateTimeFormatList(list interface{}, needs []string) []map[string]interface{} {
	data := utils.StructsToMapsJson(list)

	// 格式化时间
	for _, item := range data {
		for _, need := range needs {
			if item[need] != nil {
				item[need] = item[need].(time.Time).Format("2006-01-02 15:04:05")
			}
		}
	}

	return data
}

// @title 处理成树形数据
func treeHandler(data []map[string]interface{}, parentId uint) []map[string]interface{} {
	var list []map[string]interface{}
	for _, val := range data {
		if val["parent_id"] == parentId {
			val["children"] = treeHandler(data, val["id"].(uint))
			list = append(list, val)
		}
	}
	return list
}
